<template>
	<Form ref="form" :model="formData" label-position="top">
		<FormDescItem label="密码最小长度" desc="新用户注册时密码最小长度，0或不填为不限制，如开启免注册登录，则密码最小长度无效">
			<InputNumber v-model="formData.reg_pass_lenght" :min="0" :step="1" :precision="0"></InputNumber>
		</FormDescItem>
		<FormDescItem label="强制密码复杂度" desc="新用户注册时密码中必须存在所选字符类型，不选则为无限制">
			<CheckboxGroup v-model="formData.reg_pass_complex">
		        <Checkbox label="1">数字</Checkbox>
		        <Checkbox label="2">小写字母</Checkbox>
		        <Checkbox label="3">大写字母</Checkbox>
		        <Checkbox label="4">符号</Checkbox>
		    </CheckboxGroup>
		</FormDescItem>
		<FormDescItem label="免注册登录设置" desc="设置是否允许开启免注册登录，如开启则用户只能通过手机号码及短信验证码登录">
			<RadioGroup v-model="formData.reg_agreement">
                <Radio label="1">开启</Radio>
                <Radio label="0">关闭</Radio>
            </RadioGroup>
		</FormDescItem>
		<FormDescItem label="用户名保留关键字" desc='用户在其用户信息中无法使用这些关键字。多个关键字用键盘回车分隔，可使用通配符 "*" 如 "*管理员*"(不含引号)'>
			<InputTags v-model="formData.reg_user_censor" :filter="false" />
		</FormDescItem>
		<FormItem>
			<Button type="primary" :disabled="committing" @click="handleSubmit">{{committing ? '提交中...' : '保存设置'}}</Button>
		</FormItem>
	</Form>
</template>

<script>
import Util from '@/libs/util.js';
import FormDescItem from '@/components/form/FormDescItem.vue';
import InputTags from '@/components/form/InputTags.vue';
export default {
	components: {
		FormDescItem,
		InputTags
	},
	data() {
		return {
			committing: false,
			formData: {
				reg_pass_lenght: 0,
				reg_pass_complex: [],
				reg_agreement: '1',
				reg_user_censor: []
			}
		}
	},
	computed: {
		configure() {
			return this.$store.state.config.configure;
		}
	},
	mounted() {
		this.init();
	},
	watch: {
		configure: {
			deep: true,
			handler() {
				this.init();
			}
		}
	},
	methods: {
		init() {
			let configure = this.configure;
			if(configure && JSON.stringify(configure) != '{}') {
				this.formData.reg_pass_lenght = Number(configure.reg_pass_lenght) || 0;
				this.formData.reg_pass_complex = configure.reg_pass_complex.split(',');
				this.formData.reg_agreement = configure.reg_agreement;
				this.formData.reg_user_censor = configure.reg_user_censor.split(',');
			}
		},
		handleSubmit() {
			if(this.committing) return false;
			let form = this.formData;
        	let params = {
        		reg_pass_lenght: form.reg_pass_lenght,
				reg_pass_complex: form.reg_pass_complex.join(','),
				reg_agreement: form.reg_agreement,
				reg_user_censor: form.reg_user_censor.join(',')
        	}
        	this.committing = true;
        	Util.ajax.post(this.API['API_SETTING_SAVE'], {data: params}).then(res => {
        		this.committing = false;
        		this.$Message.success('保存成功');
        		// 同步本地数据
        		let configure = this.configure;
        		for(let k in params) {
        			configure[k] = params[k];
        		}
        		this.$store.commit('setSystemConfig', configure);
        	}).catch(() => {
        		this.committing = false;
        	})
		}
	}
}
</script>